const getMap= () => {   
   let map_=new Object();  
  /** 存放键的数组(遍历用到) */    
  map_.keys = new Array();     
  /** 存放数据 */    
  map_.data = new Object();     

  /**   
   * 放入一个键值对   
   * @param {String} key   
   * @param {Object} value   
   */    
  map_.put = function(key, value) {     
      if(map_.data[key] == null){     
          map_.keys.push(key);     
      }     
      map_.data[key] = value;     
  };     

  /**   
   * 获取某键对应的值   
   * @param {String} key   
   * @return {Object} value   
   */    
  map_.get = function(key) {     
      return map_.data[key];     
  };   
  
  // todo 更新键值
  map_.update = function(key, value) {

  }

  /**   
   * 删除一个键值对   
   * @param {String} key   
   */    
  map_.remove = function(key) {     
      // map_.keys.remove(key); 
      let index = map_.keys.indexOf(key)
      if(index > -1){
        map_.keys.splice(index, 1)
      }     
      map_.data[key] = null;     
  };     

  /**   
   * 遍历Map,执行处理函数   
   *    
   * @param {Function} 回调函数 function(key,value,index){..}   
   */    
  map_.each = function(fn){     
      if(typeof fn != 'function'){     
          return;     
      }     
      let len = map_.keys.length;     
      for(let i=0;i<len;i++){     
          let k = map_.keys[i];     
          fn(k,map_.data[k],i);     
      }     
  };
  
  map_.clear = function(){
      let len = map_.keys.length
      map_.keys.splice(0, len)
      map_.data = new Object();
  }

  /**   
   * 获取键值数组(类似<a href="http://lib.csdn.net/base/java" class='replace_word' title="Java 知识库" target='_blank' style='color:#df3434; font-weight:bold;'>Java</a>的entrySet())   
   * @return 键值对象{key,value}的数组   
   */    
  map_.entrys = function() {     
      let len = map_.keys.length;     
      let entrys = new Array(len);     
      for (let i = 0; i < len; i++) {     
          entrys[i] = {     
              key : map_.keys[i],     
              value : map_.data[i]     
          };     
      }     
      return entrys;     
  };     

  /**   
   * 判断Map是否为空   
   */    
  map_.isEmpty = function() {     
      return map_.keys.length == 0;     
  };     

  /**   
   * 获取键值对数量   
   */    
  map_.size = function(){     
      return map_.keys.length;     
  };     

  /**   
   * 重写toString    
   */    
  map_.toString = function(){     
      let s = "{";     
      for(let i=0;i<map_.keys.length;i++,s+=','){     
          let k = map_.keys[i];     
          s += '"'+k+'"'+":"+'"'+map_.data[k]+'"'; 
         
      }     
      s+="}";  
     s =  s.substr(0, s.length - 2);
    s+="}"; 
      return s;     
  };
  return map_;     
}    

module.exports = {
    getMap:getMap
  }
